package com.zrx.govern.component.easyexcel.starter.handler;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.List;


/**
 * 自定义表头高度宽度
 * @author zhangxiaowei
 * @version V1.0
 **/
public class CustomHeaderCellWriteHandler implements CellWriteHandler {
 
    private final int width;
    private final int height;
 
    public CustomHeaderCellWriteHandler(int width, int height) {
        this.width = width;
        this.height = height;
    }
 

    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {
        // 设置表头所在的列宽度
        if (isHead) {
            writeSheetHolder.getSheet().setColumnWidth(columnIndex, width * 256);
        }
    }
 
    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        // 如果需要设置高度，可以在这里设置
        if (isHead) {
            // 设置表头高度
            int rowNum = writeSheetHolder.getSheet().getLastRowNum();
            Row row = writeSheetHolder.getSheet().getRow(rowNum);
            if (row == null) {
                row = writeSheetHolder.getSheet().createRow(rowNum);
            }
            row.setHeightInPoints(height);
        }
    }
}